import time
from datetime import timedelta

import re


def main():
    parse_rtf()


def match_object_end():
    pattern = r"\s*}"

    bad1 = """{ IProfile
	- _id = GUID d9689b73-885e-44c4-896b-de43defa0a33;
	- _properties = { IPropertyContainer
		- Subjects = { IRPYRawContainer
			- size = 3;
			- value =
			{ IPropertySubject
				- _Name = "CPP_CG";
				- Metaclasses = { IRPYRawContainer
					- size = 2;
					- value =
					{ IPropertyMetaclass
						- _Name = "Class";
						- Properties = { IRPYRawContainer
							- size = 1;
							- value =
							{ IProperty
								- _Name = "In";
								- _Value = "$type*";
								- _Type = String;
							}
						}
					}"""
    output(pattern, bad1)

    good1 = """
							}"""
    output(pattern, good1)


def output(pattern, content):
    match = re.search(pattern, content, re.MULTILINE+re.DOTALL)
    if match:
        print(f"re.match() found: {match}")
        text = ""
        fist_match = match[0]
        for m in fist_match:
            if len(m) > 0:
                text = m
                break

        print(text)
    else:
        print("No match")


def match_string():
    # pattern = r"(^=\s\".*\";)|(^=\s[^\";]*;)"
    pattern = r"= .*;$"

    content = """= "{A
    B
    C
    }";"""
    match_string_output(pattern, content)

    content = """= 123;"""
    match_string_output(pattern, content)

    content = """= "abc" 123;"""
    match_string_output(pattern, content)


def match_string_output(pattern, content):
    match = re.search(pattern, content, re.MULTILINE+re.DOTALL)
    if match and match.span()[0] == 0:
        # print(f"re.match() found: {match.group()}")
        text = ""
        if match.group():
            text = match.group()[2:-1]

        print(text)
    else:
        print("No match")


def parse_rtf():
    rtf = """{\\rtf1\\fbidis\\ansi\\ansicpg1255\\deff0\\deflang1037{\\fonttbl{\\f0\\froman\\fcharset0 Times New Roman;}{\\f1\\froman\\fcharset177{\\*\\fname Times New Roman;}Times New Roman (Hebrew);}{\\f2\\fnil\\fcharset0 Arial;}}
{\\colortbl ;\\red0\\green0\\blue0;}
\\viewkind4\\uc1\\pard\\ltrpar\\cf1\\f0\\fs20 A parametric diagram describes the parametric relationships among the properties associated with\\f1\\rtlch  \\lang1033\\f0\\ltrch blocks\\lang1037 , parts, parametric constraints, parametric constraints usages and items that flow between them. The parametric diagram is used to integrate behavior and structure models with engineering analysis models such as performance, reliability, and mass property models.\\cf0\\fs24\\par
\\par
\\par
\\par
\\f2\\fs20\\par
}
"""

    rtf = parse_rtf_remove(r"\\[0-9a-zA-Z]*", rtf)

    new_rtf = parse_rtf_remove(r"\{[ *a-zA-Z();]*;\}", rtf)
    while new_rtf != rtf:
        rtf = new_rtf
        new_rtf = parse_rtf_remove(r"\{[ *a-zA-Z();]*;\}", rtf)

    rtf = parse_rtf_remove(r"[{}]", rtf)
    rtf = re.sub(r"\s+", " ", rtf)
    rtf = re.sub(r"\s+,", ",", rtf)
    print(rtf.strip())


def parse_rtf_remove(pattern, content: str) -> str:
    return re.sub(pattern, "", content)


if __name__ == "__main__":
    start_time = time.time()
    main()
    end_time = time.time()
    print(timedelta(seconds=end_time - start_time))
